package com.lovi.net_.tcp;


import com.lovi.utils.StreamUtils;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * 客户端上传图片给服务器端
 * 并接收服务器端发送回来的"已经接收到图片"
 */
public class SocketTCPClient04 {
    public static void main(String[] args) throws IOException {
        //1.客户端连接到本机8099端口，得到服务端的socket
        Socket socket = new Socket(InetAddress.getLocalHost(), 8099);
        System.out.println("客户端socket返回：" + socket);
        //2.客户端上传图片给服务器端
        //2.1.先得到本机图片数据
        FileInputStream fis = new FileInputStream("D:\\Pictures\\壁纸\\1.png");
        byte[] bytes = StreamUtils.streamToBytes(fis);
        //2.2.上传给服务器
        OutputStream outputStream = socket.getOutputStream();
        //处理流
        BufferedOutputStream bos = new BufferedOutputStream(outputStream);
        bos.write(bytes);
        bos.flush();
        //结束标志
        socket.shutdownOutput();
        //3.接收服务器端发送回来的"已经接收到图片"(字符流)
        InputStream inputStream = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        while ((line = br.readLine()) != null){
            System.out.println(line);
        }
        //统一关闭流
        br.close();
        inputStream.close();
        bos.close();
        outputStream.close();
        fis.close();
        System.out.println("客户端关闭....");
    }
}
